/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.storage;

import org.apache.log4j.*;
import org.hibernate.*;
import org.hibernate.criterion.*;

import java.util.*;

/**
 * Access data about subscribers in database
 */
public class SubscriberDAO
        implements SubscriberManager
{

    Session session = null;
    private static final Logger logger = Logger.getLogger (SubscriberDAO.class);

    /**
     * Default constructor
     */
    public SubscriberDAO ()
    {
        this.session = DataBaseManager.getSessionFactory ().getCurrentSession ();
    }

    /**
     * @see lk.mrt.cse.wihidum.storage.SubscriberManager#saveSubscriber(Subscriber)
     */
    public boolean saveSubscriber (Subscriber subscriber)
    {
        try
        {
            Transaction tx = session.beginTransaction ();
            session.save (subscriber);
            tx.commit ();
            logger.info ("Subscriber saved successfully");
            return true;
        }
        catch (HibernateException ex)
        {
            logger.error ("Error in saving subscriber", ex);
            return false;
        }
    }
    public void deleteSubscriber(String subscriberIdentifier)
    {
        try
        {
            Transaction tx = session.beginTransaction ();
            ArrayList<Subscriber> searchedList;

            Criteria criteria = session.createCriteria (Subscriber.class)
                    .add (Restrictions.eq ("uuid",subscriberIdentifier));
            searchedList = (ArrayList<Subscriber>) criteria.list ();
            Subscriber subscriber =searchedList.get(0);
            session.delete(subscriber);
            tx.commit ();
            logger.info ("Subscriber deleted successfully");
            
        }
        catch(HibernateException ex)
        {
              ex.printStackTrace();
        }
    }
    /**
     * @see lk.mrt.cse.wihidum.storage.SubscriberManager#getSubscribersFilteredByTopic(String)
     */
    public ArrayList<Subscriber> getSubscribersFilteredByTopic (String topic)
    {
        try
        {
            ArrayList<Subscriber> searchedList;

            session.beginTransaction ();
            Criteria criteria = session.createCriteria (Subscriber.class)
                    .add (Restrictions.eq ("subscriberIdentifier.filterTopic", topic));
            searchedList = (ArrayList<Subscriber>) criteria.list ();

            if (searchedList == null || searchedList.isEmpty ())
            {
                logger.debug ("No registered subscribers found for topic : " + topic);
                return new ArrayList<Subscriber> ();
            }
            else
            {
                logger.debug (searchedList.size () + " registered subscriber(s) found for topic : " + topic);
                return searchedList;
            }
        }
        catch (HibernateException ex)
        {
            logger.error ("Error in searching for subscribers", ex);
            return new ArrayList<Subscriber> ();
        }
    }

    /**
     * @see lk.mrt.cse.wihidum.storage.SubscriberManager#isDuplicatedSubscriber(String)
     */
    public boolean isDuplicatedSubscriber (String subscriptionID)
    {
        try
        {
            ArrayList<Subscriber> searchedList;

            session.beginTransaction ();
            Criteria criteria = session.createCriteria (Subscriber.class);
            searchedList = (ArrayList<Subscriber>) criteria.list ();

            if (searchedList == null || searchedList.isEmpty ())
            {
                return false;
            }
            else
            {
                for (Subscriber subscriber : searchedList)
                {
                    //if (subscriber.getClientID ().equals (subscriptionID))
                    {
                        return true;
                    }
                }
                return false;
            }
        }
        catch (HibernateException ex)
        {
            logger.error ("Error in searching for subscribers", ex);
            return false;
        }
    }
}

